<template>
  <!-- 患者档案 -->
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" :permission="permissionList"
      :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave" @row-del="rowDel"
      @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange"
      @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">添加
        </el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button type="text" size="small" icon="el-icon-view" @click.stop="handleDetail(scope.row)">详情
        </el-button>
        <el-button type="text" size="small" icon="el-icon-edit" @click.stop="handleUpdate(scope.row)">编辑
        </el-button>
      </template>

    </avue-crud>
  </basic-container>
</template>

<script>
import { getList, getDetail, add, update, remove } from "@/api/infoManagement/patientFile";
import { mapGetters } from "vuex";
import website from "@/config/website";
import { getAge } from '@/util/date.js'
// const statusList = [
// {
//   label: '未激活',
//   value: 0,
// },
// {
//   label: '在线',
//   value: 1,
// },
// {
//   label: '离线',
//   value: 2,
// },
export default {

  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 5,
        searchMenuSpan: 5,
        border: true,
        index: true,
        viewBtn: false,
        editBtn: false,
        addBtn: false,
        selection: false,
        refreshBtn: false,
        columnBtn: false,
        searchShowBtn: false,
        dialogClickModal: false,
        column: [
          {
            label: "患者姓名",
            prop: "name",
            search: true,

          },
          {
            label: "性别",
            prop: "sex",
            type: "select",
            search: true,
            props: {
              label: 'name',
              value: 'code'
            },
            dicData: [{
              name: '男',
              code: 1
            }, {
              name: '女',
              code: 0
            }],
            rules: [{
              required: true,
              message: "请选择",
              trigger: "blur"
            }]
          },
          {
            label: "出生日期",
            prop: "birthday",
            type: 'date',
            hide: true,
            valueFormat: 'yyyy-MM-dd',
          },
          {
            label: "民族",
            prop: "nation",
            hide: true,
          },
          {
            label: "身份证号",
            prop: "nation",
            hide: true,
          },
          {
            label: "身高",
            prop: "nation",
            hide: true,
            type: 'number',
          },
          {
            label: "体重",
            prop: "nation",
            hide: true,
            type: 'number',
          }, {
            label: "年龄",
            prop: "birthday",
            search: true,
            formatter(val, value) {
              return getAge(value)
            }
          },
          {
            label: "联系人",
            prop: "nation",
            hide: true,
          },
          {
            label: "联系电话",
            prop: "nation",
            hide: true,
          },
          {
            label: '建档时间',
            prop: 'createTime',
            type: 'datetime',
            addDisplay: false,
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            rules: [
              {
                required: true,
                message: '请选择',
                trigger: 'blur'
              }
            ]

          },
          {
            label: "常住地址",
            prop: "address",
            type: "textarea",
            span: 24,
            minRows: 6,

          },
          {
            label: "状态",
            prop: "status",
            type: "select",
            search: true,
            props: {
              label: 'name',
              value: 'code'
            },
            dicData: [{
              name: '待检查',
              code: 1
            }, {
              name: '待诊断',
              code: 2
            }, {
              name: '待复查',
              code: 3
            }, {
              name: '康复中',
              code: 4
            }, {
              name: '已康复',
              code: 5
            }],
            rules: [{
              required: true,
              message: "请选择",
              trigger: "blur"
            }]
          },

        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        // addBtn: this.vaildData(this.permission.post_add, false),
        // viewBtn: this.vaildData(this.permission.post_view, false),
        // delBtn: this.vaildData(this.permission.post_delete, false),
        // editBtn: this.vaildData(this.permission.post_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    handleAdd(row) {
      // const statusNew = statusList.find((x) => {
      //   if (x.value == row.offLineStatus) {
      //     return true
      //   }
      // })

      this.$router.push({
        path: '/patientFile/add',
      })
    },
    handleDetail(row) {
      this.$router.push({
        path: '/patientFile/detail',
        query: {
          id: row.id,
        },
      })
    },
    handleUpdate(row) {
      this.$router.push({
        path: '/patientFile/edit',
        query: {
          id: row.id,
        },
      })
    },
    rowSave(row, done, loading) {
      add(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style></style>
